<template>
  <el-dialog title="工单详情" :visible.sync="dialogVisible" width="80%">
    <!--  电脑端搜索框  -->
    <el-form
      class="none"
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="工单类型" prop="taskType">
        <el-select
          v-model="queryParams.taskType"
          placeholder="请选择工单类型"
          clearable
          size="small"
          filterable
        >
          <el-option
            v-for="dict in dict.type.task_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="实施时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-select
          size="small"
          v-model="queryParams.customerName"
          placeholder="请选择客户"
          filterable
          clearable
        >
          <el-option
            v-for="customer in customerList"
            :key="customer.id"
            :label="customer.name"
            :value="customer.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商机名称" prop="orderName">
        <el-input
          v-model="queryParams.orderName"
          placeholder="请输入商机名称"
          clearable
          size="small"
        />
      </el-form-item>

      <el-form-item label="负责人" prop="userList">
        <el-select
          style="width: 100%"
          v-model="queryParams.createBy"
          placeholder="请选择"
          size="small"
          clearable
          filterable
        >
          <el-option
            v-for="owner in userList"
            :key="owner.userName"
            :label="owner.userName"
            :value="owner.userName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <span style="margin-left: 10px">
          <el-divider direction="vertical"></el-divider>
          <span>工单天数合计：{{ amount }} 天</span>
        </span>
      </el-form-item>
    </el-form>

    <!--  查询列表  -->
    <el-table
      stripe
      ref="tableForm"
      class="none"
      v-loading="loading"
      :data="taskList"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="工单编号"
        class-name="clickText"
        align="left"
        prop="id"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="createBy" />
      <el-table-column label="工单类型" align="center" prop="taskType">
        <template slot-scope="scope">
          <span
            v-if="scope.row.taskType === '维保工单'"
            @click.stop="opinionType(scope.row)"
            class="type"
            >{{ scope.row.taskType }}</span
          >
          <span v-else>{{ scope.row.taskType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="关联客户" align="center" prop="customerName" />
      <el-table-column label="关联商机" align="center" prop="orderName" />
      <el-table-column
        label="实施开始时间"
        align="center"
        prop="startTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="实施结束时间"
        align="center"
        prop="endTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际天数" align="center" prop="actualDay" />
      <el-table-column label="紧急程度" align="center" prop="taskLevel" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            v-if="scope.row.status != 1"
            :options="dict.type.common_status"
            :value="scope.row.status"
          />
          <span
            v-if="scope.row.status == 1"
            style="color: #00afff; text-decoration: underline"
            @click="openAudit()"
            >待审批</span
          >
        </template>
      </el-table-column>
    </el-table>

    <!--  分页查询  -->
    <pagination
      class="none"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </el-dialog>
</template>

<script>
// import { listByOrder } from "@/api/crm/task";
import { listCustomer } from "@/api/crm/customer";
import { listOrder } from "@/api/crm/order";
// import { listOwner } from "@/api/crm/owner";
import { checkPermi } from "@/utils/permission";

// import { getTaskDetails } from "@/api/crm/dataStatistics";
export default {
  name: "Task",
  dicts: [
    "task_level",
    "task_type",
    "task_mode",
    "common_status",
    "company_name",
    "service_evaluation",
  ],
  data() {
    return {
      dialogVisible: false,
      amount: 0,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 工单表格数据
      taskList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskType: null,
        startTime: null,
        endTime: null,
        taskLevel: null,
        type: 1,
      },
      dateRange: [],
      // 查询人员
      queryUser: {
        userId: null,
      },
      // 表单参数
      form: {},
      orderList: [],
      userList: [],
      customerList: [],
      maintenanceList: [],
    };
  },
  methods: {
    open(query, createBy, type) {
      debugger
      if (type == "1") {
        this.queryParams.orderId = query;
        this.queryParams.createBy = createBy
        this.getList();
      } else {
        this.queryParams.createBy = query.createBy;
        this.queryParams.isGroupData = query.isGroupData;
        this.queryParams.year = query.year;
        this.queryParams.taskType = query.taskType;
        this.getTaskDetails();
      }
      this.getOwnerList();
      this.getPersonCustomerList();
      this.dialogVisible = true;
    },
    getTaskDetails() {
      // this.loading = true;
      // this.amount = 0;
      // getTaskDetails(this.addDateRange(this.queryParams, this.dateRange)).then(
      //   (response) => {
      //     this.taskList = response.rows;
      //     this.total = response.total;
      //     this.loading = false;
      //     this.drawer = false;

      //     this.taskList.forEach((i) => {
      //       this.amount = this.amount + i.actualDay;
      //     });
      //   }
      // );
    },
    /** 查询客户列表 */
    getPersonCustomerList() {
      this.loading = true;
      let query = {
        queryType: 2,
      };
      listCustomer(query).then((response) => {
        this.customerList = response.rows;
        this.loading = false;
      });
    },

    checkPermi,

    /** 查询负责人列表 */
    getOwnerList() {
      // listOwner(this.queryParams).then((response) => {
      //   this.userList = response.data;
      // });
    },
    /** 查询工单列表 */
    getList() {
      this.loading = true;
      this.amount = 0;
      // listByOrder(this.addDateRange(this.queryParams, this.dateRange)).then(
      //   (response) => {
      //     this.taskList = response.rows;
      //     this.total = response.total;
      //     this.loading = false;
      //     this.drawer = false;

      //     this.taskList.forEach((i) => {
      //       this.amount = this.amount + i.actualDay;
      //     });
      //   }
      // );
    },
    // 取消按钮
    cancel() {
      this.openApp = false;
      this.signType = 0;
      this.openService = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskNo: null,
        orderId: null,
        taskType: null,
        phone: null,
        address: null,
        startTime: null,
        endTime: null,
        actualDay: null,
        taskLevel: "普通",
        coPerson: null,
        taskPrice: null,
        files: null,
        illustrate: null,
        salespersonId: null,
        receivablePayable: "0",
        receivablePaid: "0",
        remark: null,
        taskMode: "现场",
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
      this.resetForm("formService");
      this.finishFlag = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },

  /** 查询商机列表 */
  getOrderList() {
    this.form.orderId = null;
    if (this.form.customerId != null) {
      let data = this.customerList.find((i) => i.id == this.form.customerId);
      this.form.phone = data.linkman + " " + data.phone;
    }

    let query = {
      queryType: 2,
      customerId: this.form.customerId,
    };
    listOrder(query).then((response) => {
      this.orderList = response.rows;
    });
  },
};
</script>



